﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SolveCranks.CApSCSolveCranksNotificationHandler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SolveCranks.Resources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SolveCranks;

public class CApSCSolveCranksNotificationHandler
{
  private readonly List<CApSCSolveCranksAlgorithm.Result> _algorithmResults;
  private readonly CApSCSolveCranksNotificationHandler.Statistics _statistics;
  private CApSCSolveCranksNotificationHandler.Notification _notification;
  private CApSCSolveCranksLogWriter _logWriter;
  private bool _targetObjectsSelectedOnly;

  public CApSCSolveCranksNotificationHandler(
    List<CApSCSolveCranksAlgorithm.Result> algorithmResults,
    bool targetObjectsSelectedOnly)
  {
    this._algorithmResults = algorithmResults;
    this._targetObjectsSelectedOnly = targetObjectsSelectedOnly;
    this._statistics = this.CalculateStatistics();
  }

  public void Notify()
  {
    if (this._notification == null)
      this._notification = this.PrepareNotification();
    if (this._logWriter == null)
      this.SaveLog();
    this.ShowNotification();
  }

  public CApSCSolveCranksNotificationHandler.Notification PrepareNotification()
  {
    if (this._notification == null)
    {
      this._notification = new CApSCSolveCranksNotificationHandler.Notification();
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults1 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Success];
      int warningsCount = this.GetWarningsCount(this._statistics, CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Success);
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults2 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Failiure];
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults3 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Skip];
      if (classifierToResults2.Count > 0)
      {
        if (classifierToResults2.Count == 1)
          this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgFailToSolveSingleComponent, (object) ((ITxObject) classifierToResults2[0].Object).Name));
        else
          this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgFailToSolveMultipleComponents, (object) classifierToResults2.Count));
      }
      if (classifierToResults1.Count > 0)
      {
        if (classifierToResults1.Count == 1)
        {
          switch (warningsCount)
          {
            case 0:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveSingleComponent, (object) ((ITxObject) classifierToResults1[0].Object).Name));
              break;
            case 1:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveSingleComponentWithOneWarning, (object) ((ITxObject) classifierToResults1[0].Object).Name));
              break;
            default:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveSingleComponentWithMultipleWarnings, (object) ((ITxObject) classifierToResults1[0].Object).Name, (object) warningsCount));
              break;
          }
        }
        else
        {
          switch (warningsCount)
          {
            case 0:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveMultipleComponents, (object) classifierToResults1.Count));
              break;
            case 1:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveMultipleComponentsWithOneWarning, (object) classifierToResults1.Count));
              break;
            default:
              this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSuccessToSolveMultipleComponentsWithMultipleWarnings, (object) classifierToResults1.Count, (object) warningsCount));
              break;
          }
        }
      }
      if (classifierToResults3.Count > 0)
      {
        if (classifierToResults3.Count == 1)
          this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSkipToSolveSingleComponent, (object) ((ITxObject) classifierToResults3[0].Object).Name));
        else
          this._notification.Text.AppendLine(string.Format(CApSCSolveCranksResources.MsgSkipToSolveMultipleComponents, (object) classifierToResults3.Count));
      }
      if (classifierToResults3.Count > 0 || classifierToResults2.Count > 0 || !this._targetObjectsSelectedOnly || warningsCount > 0)
      {
        this._notification.Text.Append(Environment.NewLine + CApSCSolveCranksResources.MsgSeeLogQuestion);
        this._notification.Type = CApSCSolveCranksNotificationHandler.Notification.eType.AskForOpenLog;
        this._notification.Icon = classifierToResults2.Count > 0 ? MessageBoxIcon.Hand : MessageBoxIcon.Question;
      }
      else
      {
        this._notification.Type = CApSCSolveCranksNotificationHandler.Notification.eType.Information;
        this._notification.Icon = MessageBoxIcon.Asterisk;
      }
    }
    return this._notification;
  }

  public void SaveLog()
  {
    if (this._logWriter != null)
      return;
    try
    {
      this._logWriter = new CApSCSolveCranksLogWriter(Path.GetTempPath() + $"SolveNXKinematics_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.log");
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults1 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Success];
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults2 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Failiure];
      List<CApSCSolveCranksAlgorithm.Result> classifierToResults3 = this._statistics.ClassifierToResultsMap[CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Skip];
      List<CApSCSolveCranksAlgorithm.Result> resultsWithWarnings = this.GetAllResultsWithWarnings(this._statistics);
      this._logWriter.WriteSection(CApSCSolveCranksLogWriter.eSection.SuccessfullActions);
      this._logWriter.WriteEmptyLine();
      foreach (CApSCSolveCranksAlgorithm.Result result in classifierToResults1)
        this._logWriter.WriteAlgorithmResult(result);
      if (classifierToResults1.Count > 0)
        this._logWriter.WriteEmptyLine();
      this._logWriter.WriteSection(CApSCSolveCranksLogWriter.eSection.Warnings);
      this._logWriter.WriteEmptyLine();
      int num1 = 0;
      foreach (CApSCSolveCranksAlgorithm.Result result in resultsWithWarnings)
      {
        int num2 = this._logWriter.WriteAlgorithmResultWarnings(result);
        num1 += num2;
      }
      foreach (CApSCSolveCranksAlgorithm.Result result in classifierToResults3)
        this._logWriter.WriteAlgorithmResult(result);
      if (classifierToResults3.Count > 0 || num1 > 0)
        this._logWriter.WriteEmptyLine();
      this._logWriter.WriteSection(CApSCSolveCranksLogWriter.eSection.Errors);
      this._logWriter.WriteEmptyLine();
      foreach (CApSCSolveCranksAlgorithm.Result result in classifierToResults2)
        this._logWriter.WriteAlgorithmResult(result);
      if (classifierToResults2.Count > 0)
        this._logWriter.WriteEmptyLine();
      this._logWriter.WriteSection(CApSCSolveCranksLogWriter.eSection.Final);
      this._logWriter.WriteEmptyLine();
      int count = classifierToResults2.Count;
      int num3 = classifierToResults3.Count + num1;
      this._logWriter.WriteSection(CApSCSolveCranksLogWriter.eSection.Summary, CApSCSolveCranksResources.CommandName, count.ToString(), num3.ToString());
      this._logWriter.Close();
    }
    catch (Exception ex)
    {
      if (this._logWriter == null)
        return;
      this._logWriter.Dispose();
      this._logWriter = (CApSCSolveCranksLogWriter) null;
    }
  }

  public void Clear()
  {
    this._notification = (CApSCSolveCranksNotificationHandler.Notification) null;
    if (this._logWriter == null)
      return;
    this._logWriter.Dispose();
    this._logWriter = (CApSCSolveCranksLogWriter) null;
  }

  private void ShowNotification()
  {
    switch (this._notification.Type)
    {
      case CApSCSolveCranksNotificationHandler.Notification.eType.Information:
        int num = (int) TxMessageBox.Show(this._notification.Text.ToString(), CApSCSolveCranksResources.CommandName, MessageBoxButtons.OK, this._notification.Icon);
        break;
      case CApSCSolveCranksNotificationHandler.Notification.eType.AskForOpenLog:
        if (this._logWriter == null)
          break;
        this._logWriter.Show(CApSCSolveCranksResources.CommandName, this._notification.Text.ToString(), this._notification.Icon);
        break;
    }
  }

  private CApSCSolveCranksNotificationHandler.Statistics CalculateStatistics()
  {
    CApSCSolveCranksNotificationHandler.Statistics statistics = new CApSCSolveCranksNotificationHandler.Statistics();
    foreach (CApSCSolveCranksAlgorithm.Result algorithmResult in this._algorithmResults)
    {
      CApSCSolveCranksNotificationHandler.Statistics.eClassifier key = this.Classify(algorithmResult);
      statistics.ClassifierToResultsMap[key].Add(algorithmResult);
    }
    return statistics;
  }

  private CApSCSolveCranksNotificationHandler.Statistics.eClassifier Classify(
    CApSCSolveCranksAlgorithm.Result result)
  {
    CApSCSolveCranksNotificationHandler.Statistics.eClassifier eClassifier = CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Success;
    switch (result.Status)
    {
      case CApSCSolveCranksAlgorithm.Result.eStatus.Unknown:
      case CApSCSolveCranksAlgorithm.Result.eStatus.CannotOpenForKinematicsModeling:
      case CApSCSolveCranksAlgorithm.Result.eStatus.Failiure:
      case CApSCSolveCranksAlgorithm.Result.eStatus.EndModelingFailed:
      case CApSCSolveCranksAlgorithm.Result.eStatus.ReloadComponentFailed:
        eClassifier = CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Failiure;
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.AlreadyOK:
        eClassifier = CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Skip;
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.Success:
        eClassifier = CApSCSolveCranksNotificationHandler.Statistics.eClassifier.Success;
        break;
    }
    return eClassifier;
  }

  private int GetWarningsCount(
    CApSCSolveCranksNotificationHandler.Statistics statistics,
    CApSCSolveCranksNotificationHandler.Statistics.eClassifier classifier)
  {
    int warningsCount = 0;
    foreach (CApSCSolveCranksAlgorithm.Result result in statistics.ClassifierToResultsMap[classifier])
    {
      if (result.Warnings != null)
        warningsCount += result.Warnings.Count;
    }
    return warningsCount;
  }

  private List<CApSCSolveCranksAlgorithm.Result> GetResultsWithWarnings(
    CApSCSolveCranksNotificationHandler.Statistics statistics,
    CApSCSolveCranksNotificationHandler.Statistics.eClassifier classifier)
  {
    List<CApSCSolveCranksAlgorithm.Result> resultsWithWarnings = new List<CApSCSolveCranksAlgorithm.Result>();
    foreach (CApSCSolveCranksAlgorithm.Result result in statistics.ClassifierToResultsMap[classifier])
    {
      if (result.Warnings != null && result.Warnings.Count > 0)
        resultsWithWarnings.Add(result);
    }
    return resultsWithWarnings;
  }

  private List<CApSCSolveCranksAlgorithm.Result> GetAllResultsWithWarnings(
    CApSCSolveCranksNotificationHandler.Statistics statistics)
  {
    List<CApSCSolveCranksAlgorithm.Result> resultsWithWarnings1 = new List<CApSCSolveCranksAlgorithm.Result>();
    for (int classifier = 0; classifier < 3; ++classifier)
    {
      List<CApSCSolveCranksAlgorithm.Result> resultsWithWarnings2 = this.GetResultsWithWarnings(statistics, (CApSCSolveCranksNotificationHandler.Statistics.eClassifier) classifier);
      resultsWithWarnings1.AddRange((IEnumerable<CApSCSolveCranksAlgorithm.Result>) resultsWithWarnings2);
    }
    return resultsWithWarnings1;
  }

  public class Notification
  {
    public CApSCSolveCranksNotificationHandler.Notification.eType Type;
    public StringBuilder Text = new StringBuilder();
    public MessageBoxIcon Icon;

    public enum eType
    {
      Information,
      AskForOpenLog,
    }
  }

  public class Statistics
  {
    public Dictionary<CApSCSolveCranksNotificationHandler.Statistics.eClassifier, List<CApSCSolveCranksAlgorithm.Result>> ClassifierToResultsMap;

    public Statistics()
    {
      this.ClassifierToResultsMap = new Dictionary<CApSCSolveCranksNotificationHandler.Statistics.eClassifier, List<CApSCSolveCranksAlgorithm.Result>>(3);
      for (int key = 0; key < 3; ++key)
        this.ClassifierToResultsMap.Add((CApSCSolveCranksNotificationHandler.Statistics.eClassifier) key, new List<CApSCSolveCranksAlgorithm.Result>());
    }

    public enum eClassifier
    {
      Success,
      Failiure,
      Skip,
      Count,
    }
  }
}
